package com.tinem.platform.module.starter.web.filter;

import com.tinem.platform.module.starter.web.context.GatewayContext;

import javax.servlet.*;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

/**
 * @author fengzhihao
 * @version v1
 * @program: platform
 * @className GatewayFilter
 * @description TODO
 * @site 
 * @company 
 * @create 2020-07-04 10:09
 */
public class GatewayFilter implements Filter {
    @Override
    public void init(FilterConfig filterConfig) {

    }
    @Override
    public void destroy() {

    }
    @Override
    public void doFilter(ServletRequest servletRequest, ServletResponse servletResponse, FilterChain filterChain) throws IOException, ServletException {
        final HttpServletRequest request = (HttpServletRequest) servletRequest;
        final HttpServletResponse response = (HttpServletResponse) servletResponse;
        GatewayContext.REQUEST_LOCAL.set(request);
        GatewayContext.RESPONSE_LOCAL.set(response);
        try {
            filterChain.doFilter(request,response);
        }finally {
            GatewayContext.REQUEST_LOCAL.remove();
            GatewayContext.RESPONSE_LOCAL.remove();
        }

    }
}
